#pragma once
#include <functional>
#include "reactor.hpp"
/*
    一个文件描述符，一个链接对象
*/
class Reactor;
class Connection;
using func_t = std::function<void(Connection *)>;
class Connection
{
public:
    Connection(int fd, uint32_t event, func_t recver, func_t sender, func_t excepter, Reactor *reactor)
        : _fd(fd), _event(event), _recver(recver), _sender(sender), _excepter(excepter), _reactor(reactor)
    {
    }

    int getConnectionFd()
    {
        return _fd;
    }

    void appendInbuff(const std::string& s)
    {
        _inbuff += s;
    }

    void appendOutbuf(const std::string& s)
    {
        _outbuff += s;
    }

    bool isEmptyOutbuf()
    {
        return _outbuff.size() == 0;
    }

    void eraseOutbuff(int n)
    {
        _outbuff.erase(0, n);
    }

    std::string& getInbuff()
    {
        return _inbuff;
    }

    std::string& getOutbuff()
    {
        return _outbuff;
    }

    void setEvent(uint32_t event)
    {
        _event = event;
    }
    
private:
    int _fd;         // 要监听的文件描述符
    uint32_t _event; // 要监听的事件

    std::string _inbuff; // 输入缓冲区
    std::string _outbuff; // 输出缓冲区

public:
    func_t _recver;    // 读事件就绪时回调方法(IO+上层业务处理)
    func_t _sender;    // 写事件就绪时回调方法(IO+上层业务处理)
    func_t _excepter;  // 异常事件就绪时回调方法
    Reactor *_reactor; // 回指指针，指向reactor对象(listen socket中需要将获取的新连接添加到reactor中)
};